<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回对象或类的父类名</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.get-parent-class" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">get_parent_class</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">get_parent_class</span> &mdash; <span class="dc-title">返回对象或类的父类名</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.get-parent-class-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>get_parent_class</strong></span>
    ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$obj</code></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   如果 <code class="parameter">obj</code>
   是对象，则返回对象实例 <code class="parameter">obj</code>
   所属类的父类名。
  </p>
  <p class="para">
   如果 <code class="parameter">obj</code>
   是字符串，则返回以此字符串为名的类的父类名。此功能是在
   PHP 4.0.5 中增加的。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    自 PHP 5 起，如果在对象的方法内调用，则 <code class="parameter">obj</code> 为可选项。
   </p>
  </p></blockquote>
  <p class="para">
   <div class="example" id="example-6144">
    <p><strong>Example #1 使用 <span class="function"><strong>get_parent_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">dad&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">dad</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;implements&nbsp;some&nbsp;logic<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">child&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">dad&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">child</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I'm&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_parent_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"'s&nbsp;son\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">child2&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">dad&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">child2</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I'm&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_parent_class</span><span style="color: #007700">(</span><span style="color: #DD0000">'child2'</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"'s&nbsp;son&nbsp;too\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">child</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">child2</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
I&#039;m dad&#039;s son
I&#039;m dad&#039;s son too
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   参见 <span class="function"><a href="get_class.html" class="function">get_class()</a></span> 和
   <span class="function"><a href="is_subclass_of.html" class="function">is_subclass_of()</a></span>。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.get-parent-class-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">object</code></dt>

     <dd>

      <p class="para">
       The tested object or class name
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.get-parent-class-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the name of the parent class of the class of which
   <code class="parameter">object</code> is an instance or the name.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If the object does not have a parent or the class given does not exist <strong><code>FALSE</code></strong> will be returned.
   </p>
  </p></blockquote>
  <p class="para">
   If called without parameter outside object, this function returns <strong><code>FALSE</code></strong>.
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.get-parent-class-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>Before 5.1.0</td>
       <td>
        If called without parameter outside object, this function would have
        returned <strong><code>NULL</code></strong> with a warning.
       </td>
      </tr>

      <tr>
       <td>Since 5.0.0</td>
       <td>
        The <code class="parameter">object</code> parameter is optional if called
        from the object&#039;s method.
       </td>
      </tr>

      <tr>
       <td>Since 4.0.5</td>
       <td>
        If <code class="parameter">object</code> is a string, returns the name of the
        parent class of the class with that name.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.get-parent-class-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6145">
    <p><strong>Example #2 Using <span class="function"><strong>get_parent_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">dad&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">dad</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;implements&nbsp;some&nbsp;logic<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">child&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">dad&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">child</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I'm&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_parent_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"'s&nbsp;son\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">child2&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">dad&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">child2</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I'm&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_parent_class</span><span style="color: #007700">(</span><span style="color: #DD0000">'child2'</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"'s&nbsp;son&nbsp;too\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">child</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">child2</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
I&#039;m dad&#039;s son
I&#039;m dad&#039;s son too
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.get-parent-class-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="get_class.html" class="function" rel="rdfs-seeAlso">get_class()</a> - 返回对象的类名</span></li>
    <li class="member"><span class="function"><a href="is_subclass_of.html" class="function" rel="rdfs-seeAlso">is_subclass_of()</a> - 如果此对象是该类的子类，则返回 TRUE</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>